import '@babel/polyfill';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import gsap from 'gsap';
import * as dat from 'dat.gui';

//点材质创建雪花

//场景
const scene = new THREE.Scene();

//相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 5);
camera.position.set(0, 0, 20);
scene.add(camera);

//渲染器
const renderer = new THREE.WebGL1Renderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);



//点mesh
function creatPoints(url) {
    const bufferGeometry = new THREE.BufferGeometry();
    const number = 10000;
    const vertices = new Float32Array(number * 3);
    const color = new Float32Array(number * 3);
    for (let i = 0; i < number * 3; i++) {
        vertices[i] = Math.random() * 50 - 25;
        color[i] = Math.random();
    };
    bufferGeometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
    bufferGeometry.setAttribute('color', new THREE.BufferAttribute(color, 3));

    //纹理
    const texture = new THREE.TextureLoader().load(`./particles/${url}.png`);

    const material = new THREE.PointsMaterial({
        size: 0.5,
        vertexColors: true,
        //map:texture,
        alphaMap: texture,
        transparent: true,
        depthWrite: false//透明贴图的透明对深度模型有影响嘛
    });
    const points = new THREE.Points(bufferGeometry, material);
    scene.add(points);
    return points;
}
const points1 = creatPoints('xuehua');
const points2 = creatPoints('xuehua1');
const points3 = creatPoints('xuehua2');


//控制
const orbitControls = new OrbitControls(camera, renderer.domElement);
orbitControls.enableDamping = true;

//坐标轴
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);


//帧动画
const clock = new THREE.Clock();
function animate() {
    const time = clock.getElapsedTime();
    points1.rotation.x = time * 0.1;
    points1.rotation.y = time * 0.03;
    points2.rotation.x = time * 0.2;
    points3.rotation.x = time * 0.15;
    points3.rotation.y = -time * 0.02;
    orbitControls.update();
    renderer.render(scene, camera);
    requestAnimationFrame(animate);
};
animate();

//窗口调整侦听
window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
});


